package com.mooc.ppjoke.ui.detail;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.mooc.lib_common.PixUtils;
import com.mooc.ppjoke.data.Comment;
import com.mooc.ppjoke.databinding.LayoutFeedCommentListItemBinding;
import com.mooc.ppjoke.extention.AbsPagedListAdapter;
import com.mooc.ppjoke.ui.login.QQUserManager;

import org.jetbrains.annotations.NotNull;

/**
 * @ClassName: FeedCommentAdapter$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/26 08:27
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class FeedCommentAdapter extends AbsPagedListAdapter<Comment,FeedCommentAdapter.ViewHolder> {
    protected FeedCommentAdapter() {
        super(new DiffUtil.ItemCallback<Comment>() {
            @Override
            public boolean areItemsTheSame(@NonNull @NotNull Comment oldItem, @NonNull @NotNull Comment newItem) {
                return oldItem.id== newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull @NotNull Comment oldItem, @NonNull @NotNull Comment newItem) {
                return oldItem.equals(newItem);
            }
        });
    }

    @Override
    protected ViewHolder onCreateViewHolder2(ViewGroup parent, int viewType) {
        LayoutFeedCommentListItemBinding inflate = LayoutFeedCommentListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);

        return new ViewHolder(inflate.getRoot(),inflate);
    }

    @Override
    protected void onBindViewHolder2(ViewHolder holder, int position) {
        Comment item = getItem(position);
        holder.bindData(item);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private LayoutFeedCommentListItemBinding binding;

        public ViewHolder(@NonNull @NotNull View itemView, LayoutFeedCommentListItemBinding binding) {
            super(itemView);
            this.binding = binding;
        }

        public void bindData(Comment item) {
            binding.setComment(item);
            boolean self = item.author != null && QQUserManager.getInstance().getUserId() == item.author.getUserId();
            binding.labelAuthor.setVisibility(self ? View.VISIBLE : View.GONE);
            binding.commentDelete.setVisibility(self ? View.VISIBLE : View.GONE);
            if(!TextUtils.isEmpty(item.imageUrl)){
                binding.commentExt.setVisibility(View.VISIBLE);
                binding.commentCover.setVisibility(View.VISIBLE);
                binding.commentCover.bindData(item.width, item.height, 0, PixUtils.dp2px(200), PixUtils.dp2px(200), item.imageUrl);
                if(!TextUtils.isEmpty(item.videoUrl)){
                    binding.videoIcon.setVisibility(View.VISIBLE);
                }else {
                    binding.videoIcon.setVisibility(View.GONE);
                }

            } else {
                binding.commentCover.setVisibility(View.GONE);
                binding.videoIcon.setVisibility(View.GONE);
                binding.commentExt.setVisibility(View.GONE);
            }
        }
    }
}
